# -*- coding:utf-8 -*-

from django.db import models

from Apps.common.custommodel import CustomModel
from Apps.Interface.models.status import InterfaceStatus
from Apps.user.models import User

"""
  req_query: [
      {
          name: String,
          value: String,
          example: String,
          desc: String,
          required: {
              type: String,
              enum: ['1', '0'],
              default: '1'
          }
      }
  ]
  """


class Interface(CustomModel):
    interface_name = models.CharField(max_length=30, default="", verbose_name="接口名称")
    interface_type = models.CharField(max_length=4, default="1", verbose_name="接口类型(1-接口文档,2-快捷请求)")
    interface_open = models.CharField(max_length=4, default="0", verbose_name='是否公开接口,0-false 1-true，默认为0-false')
    # 状态外键
    interface_status = models.ForeignKey(to=InterfaceStatus, on_delete=models.SET(''),
                                         default='c91f2f61-3355-4931-a812-44f06f3a7aa9', verbose_name="接口状态")
    interface_tags = models.TextField(null=True, blank=True,default='', verbose_name='接口标签[""]')
    interface_description = models.TextField(null=True, blank=True,default='', verbose_name='接口描述')
    interface_method = models.CharField(null=True, blank=True, default="GET", max_length=10, verbose_name="接口请求方式")
    interface_path = models.CharField(max_length=255, default='', verbose_name="接口请求路径")
    interface_auth = models.TextField(null=True, blank=True,default='', verbose_name="接口认证")
    interface_cookie = models.TextField(null=True, blank=True,default='', verbose_name="cookie保存")
    interface_headers = models.TextField(null=True, blank=True,default='', verbose_name="请求头（列表格式）")
    interface_params = models.TextField(null=True, blank=True,default='', verbose_name="URL请求参数")
    interface_form = models.TextField(null=True, blank=True,default='', verbose_name="form表单类型")
    interface_bodyType = models.CharField(null=True, blank=True,max_length=50, default='',
                                          verbose_name="请求类型enum: [form, json,  raw, file]")
    interface_bodyData = models.TextField(null=True, blank=True,default='', verbose_name='请求body,接口文档')
    interface_body = models.TextField(null=True, blank=True,default='', verbose_name='请求body，直接保存值,快捷请求')
    interface_setting = models.TextField(null=True, blank=True,default='', verbose_name="单个接口的设置")
    responseType = models.CharField(null=True, blank=True,max_length=50, default='',
                                    verbose_name="响应类型enum: [text, json, xml, HTML, raw, file]")
    responseData = models.TextField(null=True, blank=True,default='', verbose_name='响应body,接口文档')

    # 排序默认-1
    index_num = models.IntegerField(default="-1", verbose_name="排序")

    project = models.ForeignKey(to='Project.project', on_delete=models.DO_NOTHING, related_name="project_interface",
                                db_constraint=False,
                                verbose_name="项目外键")
    interface_category = models.ForeignKey(null=True, blank=True, to='Interface.interfaceCat',
                                           on_delete=models.DO_NOTHING,
                                           related_name="interfaceCat_interface",
                                           db_constraint=False,
                                           verbose_name="分类外键")
    creator = models.ForeignKey(User, on_delete=models.DO_NOTHING, default="",
                                related_name="user_Interface_c",
                                verbose_name="创建人")
    modifier = models.ForeignKey(User, on_delete=models.DO_NOTHING, default="",
                                 related_name="user_Interface_m",
                                 verbose_name="修改人")

    class Meta:
        db_table = "interface"
        verbose_name = "接口表"
        ordering = ('-index_num', '-create_time',)

    def __str__(self):
        return self.interface_name
